﻿using System;
using System.Windows;
using System.Windows.Threading;
using System.Windows.Input;
using Microsoft.SqlServer.Management.Smo;

namespace SQLDataTransferer.ViewModel
{
    public class DetachDatabaseViewModel : SmoViewModelBase
    {
        #region Fields

        RelayCommand _detachCommand;

        #endregion

        #region Constructor(s)

        public DetachDatabaseViewModel(string displayName, System.Globalization.CultureInfo culture, Dispatcher dispatcher, Window main)
            : base(displayName, culture, dispatcher, main)
        {
        }

        #endregion

        #region Properties

        #region Commands

        public ICommand DetachCommand
        {
            get
            {
                if (this._detachCommand == null)
                    this._detachCommand = new RelayCommand((o) => this.OnDetach());

                return this._detachCommand;
            }
        }

        #endregion

        #endregion

        #region Methods

        private void OnDetach()
        {
            this.SetFocus();

            this.RunTaskDialog("Detaching database ...",
                () =>
                {
                    #region Validation

                    //this.Status = "Validating input ..."; //TODO: Translate
                    //System.Threading.Thread.Sleep(500); // Make sure the user sees the current status

                    //System.Text.StringBuilder sb = new System.Text.StringBuilder(0);
                    //if (this.SelectedHostInstance == null)
                    //    sb.AppendLine("You must select/provide an host server."); //TODO: Translate

                    //if (this.SelectedHostDatabase == null ||
                    //    string.IsNullOrEmpty(this.SelectedHostDatabase.Name))
                    //    sb.AppendLine("You must select/provide an host database."); //TODO: Translate

                    #endregion

                    //if (sb.Length > 0)
                    //    this.Status = sb.Insert(0, "Warning:\r\n").ToString(); //TODO: Translate
                    //else
                    //{
                    //    // Validation passed
                    //    this.Status = "Validation passed."; // TODO: Translate
                    //    System.Threading.Thread.Sleep(300); // Make sure the user sees the current status

                    //    // Start actual transfer
                    //    this.Status = "Detaching ..."; //TODO: Translate

                    //    // Get the startTime
                    //    TimeSpan startTime = DateTime.Now.TimeOfDay;

                    // Check if Windows or SQL authentication should be used
                    Server server;
                    if (this.CurrentOption == RadioButtonOptions.Windows)
                        server = SmoFactory.GetServer(SmoFactory.CreateConnection(this.SelectedServer.FullName));
                    else
                        server = SmoFactory.GetServer(SmoFactory.CreateConnection(this.SelectedServer.FullName, "//TODO:", "//TODO:"));
                    
                    // Detach the dabase from the server
                    SmoFactory.DetachDatabase(server, this.SelectedDb.Name);

                    // Get the time spend for the current operation
                    // TimeSpan duration = DateTime.Now.TimeOfDay - startTime;

                    //sb = new System.Text.StringBuilder("Database has been detached.").AppendLine(); //TODO: Translate                
                    //sb.Append("The current operation has taken");
                    //sb.AppendFormat(" {0} Hours", duration.Hours);
                    //sb.AppendFormat(", {0} Minutes", duration.Minutes);
                    //sb.AppendFormat(", {0} Seconds", duration.Seconds);

                    //this.Status = sb.ToString();

                    //sb = null;

                    // Remove the database that has been detached
                    //ProgressHelper.RemoveItem(this.Databases, this.SelectedHostDatabase, this.DispatcherObject);

                    // Clear the current selected database
                    this.SelectedDb = null;
                    this.OnPropertyChanged("SelectedDb");
                    //}
                });
        }

        #endregion
    }
}
